package com.zret.oasys.operation.dept;
/**
 * 通过部门经理ID查询部门
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zret.oasys.entity.Dept;

public class GetDeptByManagerid implements DeptServletOperation 
{
	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		//1编码
		response.setContentType("application/json;charset=utf-8");
		//2从jsp获取managerid，获取查询到的list
		String id = request.getParameter("managerid");
		
		//如果传输的是空值，查询所有员工
		if ("".equals(id))
		{
			//调用查询所有部门方法
			List<Dept> list = deptservice.getDepts();
			//3创建输出流
			PrintWriter out = response.getWriter();
			//4创建Gson对象
			Gson gson = new Gson();
			//5将查询的部门list转为字符串
			String jsonString = gson.toJson(list);
			//6控制台测试打印输出
			System.out.println(jsonString);
			//7传输到jsp页面
			out.print(jsonString);
			//8关闭out
			out.close();
		}
		else
		{
			//1查询部门部门
			int managerid = Integer.parseInt(id);
			List<Dept> list = deptservice.getDeptByManagerid(managerid);
			
			//2创建输出流
			PrintWriter out = response.getWriter();
			//3创建Gson对象
			Gson gson = new Gson();
			//4查询到的部门查询list转为字符串
			String jsonString = gson.toJson(list);
			//5控制台测试打印
			System.out.println(list);
			//6传输给jsp页面
			out.print(jsonString);
			//7关闭out
			out.close();
		}
	}
}
